module("ufe.util.test",package.seeall)
local http = require("ufe.resty.http")
local conf = require("ufe.config")

local ns = conf.tair_ns
local default_group = conf.tair_default_group
local groups = conf.tair_groups

function get(key, acf)
    key = key or "ufe_tengine"
    local url = "http://127.0.0.1:8009/"..key
    local hc = http:new()
    local ok, code, headers, status, body = hc:request{url = url,
        conn_timeout = 2000,
        send_timeout = 2000,
        resp_timeout = 2000,
        method = "GET",
        headers = {
            ["Host"] = "127.0.0.1:7009",
            ["Connection"] = "close",
        },

    }
    if ok and code == 200 and body then
        return body
    else
        return nil
    end
end

function check_config()
    if not ns then
        return false, "must set tair namespace in config.lua"
    end
    if not groups or type(groups) ~= 'table' then
        return false, "must set tair group in config.lua"
    end
    if not default_group then
        return false, "must set default tair server in config.lua"
    end
    return true
end

function check(acf)
    local exist = false
    for _, g in ipairs(groups) do
        if g == acf.tair then
            exist = true
            break
        end
    end
    if not exist then
        return false, "not found or wrong tair"
    end
    return true
end

